package com.example.garbage.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.garbage.pojo.Depot;
import com.example.garbage.pojo.RespBean;
import com.example.garbage.pojo.Truck;
import com.example.garbage.service.DepotService;
import com.example.garbage.service.TruckService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author Shunrai
 * @create 2024/3/15 13:46
 * @Version 1.0
 * @Description
 */
@RestController
@RequestMapping("/depot")
@ApiResponse(responseCode = "404", description = "查询失败")
@ApiResponse(responseCode = "200", description = "查询成功")
@Tag(name = "车辆出发点")
public class DepotController {

    @Autowired
    private DepotService depotService;
    @Autowired
    private TruckService truckService;

    @GetMapping("/getAll")
    @Operation(summary = "获取所有车辆出发点以及所属车辆")
    public RespBean<Depot> getAllDepot(){
        List<Depot> depots = new ArrayList<>();
        List<Depot> list1 = depotService.list();
        for (Depot depot : list1) {
            QueryWrapper wrapper = new QueryWrapper<Truck>().eq("did", depot.getGid());
            List<Truck> list = truckService.list(wrapper);
            depot.setTrucks(list);
            depots.add(depot);
        }
        return RespBean.success("查询成功",depots);
    }


    @GetMapping("/getMany/{ids}")
    @Operation(summary = "根据id获取车辆出发点以及所属车辆")
    public RespBean<Depot> getDepotByIds(@PathVariable("ids")@Parameter(description = ",分割") String ids){
        String[] split = ids.split(",");
        List<Depot> depots = new ArrayList<>();
        for (String str : split) {
            int number = Integer.parseInt(str);
            QueryWrapper wrapper = new QueryWrapper<Truck>().eq("did", number);
            List<Truck> list = truckService.list(wrapper);
            Depot depot = depotService.getById(number);
            depot.setTrucks(list);
           depots.add(depot);
        }
        return RespBean.success("查询成功",depots);
    }
}
